package com.ysg.base;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import androidx.annotation.NonNull;

import com.ysg.R;

/**
 * dialog：
 * 常用方法：setCanceledOnTouchOutside()-点击布局之外是否可以消失
 * setCancelable(boolean flag)： 设置是否可以使用物理方法键取消此对话框。
 *
 * setWidthScale():布局宽度
 * setAlpha():布局背景透明度
 * setSquare（）：窗口是否为正方形
 */
public abstract class BaseDialog extends Dialog {
    public Context mContext;
    protected float widthScale = 0.8f;
    protected float alpha = 1f;
    protected boolean isSquare = false;

    public BaseDialog(@NonNull Context context) {
        super(context, R.style.YDialogTheme);
        this.mContext = context;
    }

    public BaseDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        this.mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        initView();
        initWidthScale();
        initAnimation();
        initData();
        initListener();
    }

    public abstract int getLayoutId();

    public void initWidthScale(){
        //去掉默认圆角
        Window window = getWindow();
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//        window.setWindowAnimations(R.style.dialog_animation);//动画
        //窗体的宽高，透明度，及是否为正方形设置
        WindowManager windowManager = getWindow().getWindowManager();
        int screenWidth = windowManager.getDefaultDisplay().getWidth();
        WindowManager.LayoutParams attributes = getWindow().getAttributes();
        attributes.alpha = alpha;
        attributes.width = (int)(screenWidth * widthScale);
        if (isSquare){
            attributes.height = (int)(screenWidth * widthScale);
        }
        getWindow().setAttributes(attributes);
    }

    public void setAlpha(float alpha) {
        this.alpha = alpha;
    }

    public void setWidthScale(float widthScale){
        this.widthScale = widthScale;
    }

    public void setSquare(boolean square) {
        isSquare = square;
    }

    public abstract void initView();
    public abstract void initData();
    public abstract void initListener();

    protected void initAnimation(){

    }
}
